﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}
    <body>
        <div class="layuimini-container">
            <div class="layuimini-main">
                <div class="layui-form layuimini-form" lay-filter="adminform">
                    <div class="layui-form-item layui-hide">
                        <label class="layui-form-label required">任务组名</label>
                        <div class="layui-input-block">
                            <input type="text" id="F_JobGroup" name="F_JobGroup" maxlength="50" lay-verify="required" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-hide">
                        <label class="layui-form-label required">任务名称</label>
                        <div class="layui-input-block">
                            <input type="text" id="F_JobName" name="F_JobName" maxlength="50" value="" lay-verify="required" class="layui-input ">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">任务类型</label>
                        <div class="layui-input-block" id="F_JobType">
                        </div>
                    </div>
                    <div class="layui-form-item layui-hide">
                        <label class="layui-form-label required">文件名称</label>
                        <div class="layui-input-block">
                            <select id="F_FileName" name="F_FileName" lay-verify="required">
                                <option value="">==请选择==</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item layui-hide">
                        <label class="layui-form-label required">请求地址</label>
                        <div class="layui-input-block">
                            <input type="text" id="F_RequestUrl" name="F_RequestUrl" value="" lay-verify="required" class="layui-input ">
                        </div>
                    </div>
                    <div class="layui-form-item layui-hide">
                        <label class="layui-form-label">请求头</label>
                        <div class="layui-input-block">
                            <input type="text" id="F_RequestHeaders" name="F_RequestHeaders" class="layui-input ">
                        </div>
                    </div>
                    <div class="layui-form-item layui-hide">
                        <label class="layui-form-label">请求参数</label>
                        <div class="layui-input-block">
                            <input type="text" id="F_RequestString" name="F_RequestString" class="layui-input ">
                        </div>
                    </div>
                    <div class="layui-form-item layui-hide">
                        <label class="layui-form-label required">Cron表达式</label>
                        <div class="layui-input-block">
                            <input type="text" id="F_CronExpress" name="F_CronExpress" maxlength="50" lay-verify="required" value="" class="layui-input ">
                        </div>
                    </div>
                    <div class="layui-form-item layui-hide">
                        <label class="layui-form-label">选项</label>
                        <div class="layui-input-block">
                            <input type="checkbox" name="F_DoItNow" id="F_DoItNow" checked="" value="true" title="立即执行">
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text layui-hide">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-block">
                            <textarea id="F_Description" name="F_Description" class="layui-textarea" placeholder="请输入备注"></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item layui-hide">
                        <button class="layui-btn site-demo-active" lay-submit id="submit" lay-filter="saveBtn">确认保存</button>
                    </div>
                </div>
            </div>
        </div>

    </body>
    <script>
        layui.use(['jquery', 'form', 'laydate', 'common', 'optimizeSelectOption', 'cron'], function () {
            var form = layui.form,
                $ = layui.$,
                common = layui.common,
                cron = layui.cron,
                laydate = layui.laydate;
            var keyValue = $.request("keyValue");
            //权限字段
            common.authorizeFields('adminform');
            $(function () {
                initControl();
                if (!!keyValue) {
                    common.ajax({
                        url: "/SystemSecurity/OpenJobs/GetFormJson",
                        dataType: "json",
                        data: { keyValue: keyValue },
                        async: false,
                        success: function (data) {
                            common.val('adminform', data);
                            changeJobType(data.F_JobType);
                            cron.render({
                                elem: "#F_CronExpress",           // 绑定元素
                                value: data.F_CronExpress, // 默认值
                                done: function (cronStr) {  // 点击确定，或运行时，触发，参数为 cron 表达式字符串
                                    $("#F_CronExpress").val(cronStr);
                                },
                            });
                        }
                    });
                }
                else {
                    changeJobType(0);
                    cron.render({
                        elem: "#F_CronExpress",           // 绑定元素
                        value: "0/20 * * * * ?", // 默认值
                        done: function (cronStr) {  // 点击确定，或运行时，触发，参数为 cron 表达式字符串
                            $("#F_CronExpress").val(cronStr);
                        },
                    });
                }
                form.render();
            });
            wcLoading.close();
            function initControl() {
                $("#F_FileName").bindSelect({
                    url: "/SystemSecurity/OpenJobs/QueryLocalHandlers",
                    id: ""
                });
                $("#F_JobType").bindRadio({
                    data: top.clients.dataItems["JobType"],
                    id: "",
                });
            }
            form.on('radio(F_JobType)', function (data) {
                changeJobType(data.value);
            });
            function changeJobType(value) {
                if (value == "0") {
                    $('#F_FileName').parent().parent().removeClass('layui-hide');
                    $('#F_FileName').attr('lay-verify', 'required');
                    $('#F_RequestUrl').parent().parent().addClass('layui-hide');
                    $('#F_RequestUrl').removeAttr('lay-verify', 'required');
                    $('#F_RequestHeaders').parent().parent().addClass('layui-hide');
                    $('#F_RequestString').parent().parent().addClass('layui-hide');
                    $('#F_RequestUrl').val("");
                    $('#F_RequestHeaders').val("");
                    $('#F_RequestString').val("");
                }
                else {
                    $('#F_FileName').parent().parent().addClass('layui-hide');
                    $('#F_FileName').removeAttr('lay-verify');
                    $('#F_RequestUrl').parent().parent().removeClass('layui-hide');
                    $('#F_RequestUrl').attr('lay-verify', 'required');
                    $('#F_RequestHeaders').parent().parent().removeClass('layui-hide');
                    $('#F_RequestString').parent().parent().removeClass('layui-hide');
                    $('#F_FileName').val("");
                }
                form.render();
            }
            //select验证
            form.verify({
                required: function (value, item) {
                    var msg = "必填项不能为空";
                    value = $.trim(value);
                    var isEmpty = !value || value.length < 1;
                    // 当前验证元素是select且为空时,将页面定位至layui渲染的select处，或自定义想定位的位置
                    if (item.tagName == 'SELECT' && isEmpty) {
                        $("html").animate({
                            scrollTop: $(item).siblings(".layui-form-select").offset().top - 74
                        }, 50);
                    }
                    if (isEmpty) {
                        return msg;
                    }
                }
            });
            //监听提交
            form.on('submit(saveBtn)', function (data) {
                var postData = data.field;
                if (!postData["F_EnabledMark"]) postData["F_EnabledMark"] = false;
                common.submitForm({
                    url: "/SystemSecurity/OpenJobs/SubmitForm?keyValue=" + keyValue,
                    param: postData,
                    success: function () {
                        common.parentreload("data-search-btn");
                    }
                })
                return false;
            });
        });
    </script>

